# 1.list数据切片
# from collections import Iterable

L = ['Michael', 'Sarah', 'Tracy']
# [strat:end:n] strat开始位置 end结束位置  n每隔几个取
L[:3]  # 默认从0开始 不含尾
L[0:3]
L[-2:]  # 负数是从尾开始算
L[-2:-1]
L[::2]

# 2.数据迭代============
# 遍历list
for key in [4, 5, 4, 3, 6]:
    print("list===%s" % key)
for i, value in enumerate([4, 5, 4, 3, 6]):
    print("list带角标遍历 %s===%s" % (i, key))
# 遍历tuple
for key in (4, 5, 4, 3, 6):
    print("tuple===%s" % key)
# 遍历set
for key in ([1, 2, 3, 4]):
    print("set===%s" % key)

# 遍历dict
d = {'x': 'A', 'y': 'B', 'z': 'C'}
# 遍历key
for key in d:
    print("dict=value====%s" % key)
# 遍历value
for value in d.values():
    print("dict=value====%s" % value)
# 遍历key value
for k, v in d.items():
    print(k, '=', v)
for i, key in enumerate(d):
    print("dict带角标遍历 %s ===%s=%s" % (i, key, d.get(key)))

# 迭代多元素列表,里面的每一个小集合或者map可以视为一个元素
for x, y, z in [(1, 1, 3), (2, 4, 5), (3, 9, 9)]:
    print("多元素列表：",x, y, z)
for x, y, z in [[1, 1, 3], [2, 4, 3]]:
    print(x, y, z)
for i, e in enumerate([(1, 1, 3), (2, 4, 5), (3, 9, 9)]):
    print("多元素列表带角标遍历%s--- %s" %(i, e))

#带角标遍历只能取出角标和对应的一个元素

# 3.列表生成式============
list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[x * x for x in range(1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[x * x for x in range(1, 11) if x % 2 == 0]  # 带条件判断
[4, 16, 36, 64, 100]



# 4.迭代器
# 可以被next()函数调用并不断返回下一个值的对象称为 迭代器Iterator  generator
# 可以使用isinstance()判断一个对象是否是Iterable对象 可迭代对象  如list、tuple、dict、set、str

# 判断是否迭代对象
# print(isinstance('abc', Iterable)) # str是否可迭代
# print(isinstance([1,2,3], Iterable)) # list是否可迭代
# print(isinstance(123, Iterable)) # 整数是否可迭代

it= iter(["aa","bb","cc"])
while True:
    try:
        print("迭代器===",next(it))
    except StopIteration:
        #结束流程
        break

